import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-07-10
 * Time: 16:54
 */
public class Test48 {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode() {

        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildIt(preorder,inorder,0,preorder.length-1);
    }
    static int i = 0;
    public static TreeNode buildIt(int[] preorder, int[] inorder, int begin, int end) {
        if(begin > end) {
            return null;
        }
        TreeNode root = new TreeNode(preorder[i]);
        int index = find(inorder,begin,end,preorder[i]);
        i++;
        root.left = buildIt(preorder,inorder,begin,index-1);
        root.right = buildIt(preorder,inorder,index+1,end);
        return root;
    }

    public static int find(int[] num,int begin,int end,int key) {
        for(int i = begin; i <= end;i++) {
            if(num[i] == key) {
                return i;
            }
        }
        return -1;
    }

    public static List<Integer> levelT(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            while (size > 0) {
                TreeNode node = q.poll();
                if (node != null) {
                    ret.add(node.val);
                    q.offer(node.left);
                    q.offer(node.right);
                } else {
                    ret.add(null);
                }
                size--;
            }
        }
        //移除末尾的null
        while (ret.size() > 0 && ret.get(ret.size() - 1) == null) {
            ret.remove(ret.size() - 1);
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        int[] pre = new int[n1];
        for(int i = 0; i < n1; i++) {
            pre[i] = sc.nextInt();
        }
        int n2 = sc.nextInt();
        int[] in = new int[n2];
        for(int i = 0; i < n2; i++) {
            in[i] = sc.nextInt();
        }
        TreeNode root = buildTree(pre,in);
        List<Integer> ret = levelT(root);
        System.out.println(ret);
    }
}
